package br.com.Servlet;

import java.io.IOException;
import java.io.PrintWriter;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import br.com.Dao.EnderecoDao;
import br.com.Modelo.Endereco;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class jsonServelt
 */
@WebServlet("/admin/jsonServelt")
public class jsonServelt extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public jsonServelt() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("sdasdasd");
		// TODO Auto-generated method stub
		/*String name=null;
		
		name = "Hello "+request.getParameter("user");
		
		System.out.println(request.getParameter("user"));
		if(request.getParameter("user").toString().equals("")){
		name="Hello User";
		}
		response.setContentType("text/plain");  
		response.setCharacterEncoding("UTF-8"); 
		response.getWriter().write(name); */
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("sdasdasd");
		 PrintWriter out= response.getWriter();  
	        JSONObject json = new JSONObject();  
	        json.put("name", "Sameera Jayasekara");  
	        json.put("email", "codesstore@blogspot.com");  
	        

	        int id = Integer.parseInt(request.getParameter("campo1"));
	        
	        System.out.println(id);
	        
	        Endereco end = new EnderecoDao().getEnd(id);
	        
	        json.put("id", id);
	        json.put("cep",end.getCep());
	        json.put("cidade", end.getCidade());
	        json.put("bairro",end.getBairro());
	        json.put("numero",end.getNumero());
	        json.put("rua",end.getRua());
	        json.put("id_pagina",end.getIdPagina());
	        json.put("count",end.getCount());
	        json.put("telefone",end.getTelefone());
	        out.print(json); 
	}

}
